<?php
// 首页显示信息以及基本配置
namespace NewAdmin\Controller;
use Think\Controller;
class PlatformController extends CommonController {
    function _initialize() {
        parent::_initialize();
        $this->nav_model = D("Nav");
        $this->navcat_model =D("NavCat");
    }

    function index(){
        $user   =   D('User');
        $mall	=	D('Mall');
        $current  =   $this->userArray;
        $this->assign('current',$current);
        $this->assign('title','个人信息');
        //地区相关
        $areaArray  =  getAreaInfo();
        $this->assign('areaArray',$areaArray);
        $bclassid   =  $current['province_id']?$current['province_id']:'1';
        $subAreaArray  =  getAreaInfo('',$bclassid);
        $this->assign('subAreaArray',$subAreaArray);
        //获取职位列表
        $userLevelArray	=	user_level();
        foreach ( $userLevelArray AS $key=>$row ){
        	$stringFlag	.=	$stringFlag?'<br />Lv'.$key.'=>'.$row:'Lv'.$key.'=>'.$row;
        }
        $this->assign('stringFlag',$stringFlag);
        //获取直接上级信息
        $topUserArray	=	$user->field('nickname')->where(' id = "'.$current['buser_id'].'"')->find();
        $this->assign('topUserArray',$topUserArray['nickname']);
        //获取各订单状态数量
    	$orderCount[2]	=	$mall->where(' status = 2 AND ishidden = 0 ')->count();
    	$orderCount[3]	=	$mall->where(' status = 3 AND ishidden = 0 ')->count();
    	$orderCount[4]	=	$mall->where(' status = 4 AND ishidden = 0 ')->count();
    	$orderCount[6]	=	$mall->where(' status = 6 AND ishidden = 0 ')->count();
    	$this->assign('orderCount',$orderCount);
        $this->display();
    }
    
    
    
    
    public function webInfo(){
        $options   =   D('options');
        //获取系统配置
        $data['option_name']='web_info';
        $new_data=$options->where($data)->find();
        $config=json_decode($new_data['option_value']);
        $config=get_object_vars($config);
       
        $this->assign('data',$config);
        $this->assign('option_name',$data['option_name']);
        $this->assign('title','网站设置');
        $this->display();
    }
    
     public function webqita(){
        $options   =   D('options');
        //获取系统配置
        $data['option_name']='web_qita';
        $new_data=$options->where($data)->find();
        $config=json_decode($new_data['option_value']);
        $config=get_object_vars($config);
       
        $this->assign('data',$config);
        $this->assign('option_name',$data['option_name']);
        $this->assign('title','其他设置');
        $this->display();
    }
    public function webphone(){
        $options   =   D('options');
        //获取系统配置
        $data['option_name']='web_phone';
        $new_data=$options->where($data)->find();
        $config=json_decode($new_data['option_value']);
        $config=get_object_vars($config);
	      
        $this->assign('data',$config);
        $this->assign('option_name',$data['option_name']);
        $this->assign('title','短信设置');
        $this->display();
    }
    

     public function webpay(){
        $options   =   D('options');
        //获取微信配置
        $w_data['option_name']='wx_pay';
        $new_w_data=$options->where($w_data)->find();
        $w_config=json_decode($new_w_data['option_value']);
        $w_config=get_object_vars($w_config);
        //获取支付宝配置
        $a_data['option_name']='ali_pay';
        $new_a_data=$options->where($a_data)->find();
        $a_config=json_decode($new_a_data['option_value']);
        $a_config=get_object_vars($a_config);
        //获取支付宝转账配置
        $z_data['option_name']='z_ali_pay';
        $new_z_data=$options->where($z_data)->find();
        $z_config=json_decode($new_z_data['option_value']);
        $z_config=get_object_vars($z_config);
       
        $this->assign('w_config',$w_config);
        $this->assign('a_config',$a_config);
        $this->assign('z_config',$z_config);
     
        $this->assign('title','支付设置');
        $this->display();
    }
     public function weblogin(){
        $options   =   D('options');
        //获取wx配置
        $w_data['option_name']='wx_login';
        $new_w_data=$options->where($w_data)->find();
        $w_config=json_decode($new_w_data['option_value']);
        $w_config=get_object_vars($w_config);
        //获取qq宝配置
        $q_data['option_name']='qq_login';
        $new_q_data=$options->where($q_data)->find();
        $q_config=json_decode($new_q_data['option_value']);
        $q_config=get_object_vars($q_config);
        //获取微博配置
		$b_data['option_name']='wb_login';
        $new_b_data=$options->where($b_data)->find();
        $b_config=json_decode($new_b_data['option_value']);
        $b_config=get_object_vars($b_config);
		
        $this->assign('w_config',$w_config);
        $this->assign('b_config',$b_config);
        $this->assign('q_config',$q_config);
     
        $this->assign('title','登录设置');
        $this->display();
    }
    //更新系统各项配置
    public function webSave(){
        $options   =   D('options');
        $data =I('post.');
       
        $data_json=json_encode($data);
        $map['option_name']=$data['option_name'];
        $new_data['option_name']=$data['option_name'];
        $new_data['option_value']=$data_json;
        $new_data['autoload']=1;
        
        if($options->where($map)->find()){
            $r=$options->where($map)->save($new_data);
        }else{
            $r=$options->add($new_data);
        }
    
        if($r){
        	if($data['option_name']=='wx_pay'||$data['option_name']=='ali_pay'||$data['option_name']=='z_ali_pay'){
        		//获取支付配置
		        $w_data['option_name']='wx_pay';
		        $new_w_data=$options->where($w_data)->find();
		        $w_config=json_decode($new_w_data['option_value']);
	      		$w_config=get_object_vars($w_config);
		    	$a_data['option_name']='ali_pay';
		        $new_a_data=$options->where($a_data)->find();
		        $a_config=json_decode($new_a_data['option_value']);
	        	$a_config=get_object_vars($a_config);
	        	$z_data['option_name']='z_ali_pay';
		        $new_z_data=$options->where($z_data)->find();
		        $z_config=json_decode($new_z_data['option_value']);
	        	$z_config=get_object_vars($z_config);
		        $zhifu['weixin']=$w_config;
		        $zhifu['ali']=$a_config;
		        $zhifu['zali']=$z_config;
	     		file_put_contents('data/conf/zhifu.php', "<?php\treturn " . var_export($zhifu, true) . ";");	
        	}
        	if($data['option_name']=='web_phone'){
        		$w_data['option_name']='web_phone';
		        $new_w_data=$options->where($w_data)->find();
		        $w_phone=json_decode($new_w_data['option_value']);
	      		$w_phone=get_object_vars($w_phone);
	      		$duanxin['duanxin']=$w_phone;
	      		file_put_contents('data/conf/phone.php', "<?php\treturn " . var_export($duanxin, true) . ";");	
        	}
			if($data['option_name']=='wb_login'||$data['option_name']=='wx_login'||$data['option_name']=='qq_login'){
        		$wb_data['option_name']='wb_login';
		        $new_wb_data=$options->where($wb_data)->find();
		        $wb_login=json_decode($new_wb_data['option_value']);
	      		$wb_login=get_object_vars($wb_login);
	      		$login['wb_login']=$wb_login;
				
				$wx_data['option_name']='wx_login';
		        $new_wx_data=$options->where($wx_data)->find();
		        $wx_login=json_decode($new_wx_data['option_value']);
	      		$wx_login=get_object_vars($wx_login);
	      		$login['wx_login']=$wx_login;
				
				$qq_data['option_name']='qq_login';
		        $new_qq_data=$options->where($qq_data)->find();
		        $qq_login=json_decode($new_qq_data['option_value']);
	      		$qq_login=get_object_vars($qq_login);
	      		$login['qq_login']=$qq_login;
				
	      		file_put_contents('data/conf/login.php', "<?php\treturn " . var_export($login, true) . ";");	
        	}
        	
            echo '0|||保存成功|||reload'; die();
        }else{
            echo '0|||没有数据更新|||reload'; die();
        }
    }
    
    /**
     * 修改用户密码
    */
    function userChangePassword(){
        $this->display();
    }
    
    /**
     * 修改用户密码保存
    */
    function userPasswordSave(){
        $user   =   D('User');
        $data['password']   =   md5(I('post.password1').'ltj2016');
        if( !$_POST['password1'] || !$_POST['password2'] || !$_POST['password'] ){
        	echo '0|||请输入完整信息';die();
        }
        if( $_POST['password1'] != $_POST['password2'] ){
        	echo '0|||密码不一致';die();
        }
        $flag   =   $user->where('password = "'. md5($_POST['password'].'ltj2016').'" AND id = "'.session('user_id').'"')->save($data);
        if( $flag !== false ){
        	echo '0|||修改成功|||/Index/index/';die();
        }else{
        	echo '0|||修改失败';die();
        }
    }

    function shopnav_cat(){
        $nav=M('nav_cat');
        $cats=$nav->select();
        $this->assign("navcats",$cats);
        $this->assign('title','导航分类管理');
        $this->display();
    }
    function shopnav_cat_add(){
        if($_POST){
            $data=I('post.');
            $nav=M('nav_cat');
            $rs=$nav->add($data);
            if( $rs){
                echo '0|||添加成功|||reload';die();
            }else{
                echo '0|||添加失败|||reload';die();
            }
        }else{
            $this->assign('title','添加导航分类');
            $this->display();
        }
        
    }
    function shopnav_cat_edit(){
         $nav=M('nav_cat');
        if($_POST){
            $data=I('post.');
            $rs=$nav->save($data);
            if( $rs){
                echo '0|||修改成功|||reload';die();
            }else{
                echo '0|||没有数据更新|||reload';die();
            }
        }else{
            $id=I('get.id');
            $rs=$nav->find($id);

            $this->assign('rs',$rs);
            $this->assign('title','编辑导航分类');
            $this->display();
        }
       
    }
    function shopnav_cat_delete(){
        $nav=M('nav_cat');
        $id=I('get.id');
        $rs=$nav->delete($id);
        $this->redirect('Platform/shopnav_cat');
    }

    function shopnav(){
        if(empty($_REQUEST['cid'])&&$_SESSION['nav']['cid']==null){
            $navcat=$this->navcat_model->find();
            $cid=$navcat['navcid'];
            $_SESSION['nav']['cid']=$cid;
        }else{
            if($_REQUEST['cid']==null){
                $cid=$_SESSION['nav']['cid'];
            }else{
                $cid=$_REQUEST['cid'];
                $_SESSION['nav']['cid']=$cid;
            }
        
        }
        
        
        $result = $this->nav_model->where("cid=$cid")->order(array("listorder" => "ASC"))->select();
        import("Think.Tree");
        $tree = new \Tree();
        $tree->icon = array('&nbsp;&nbsp;&nbsp;│ ', '&nbsp;&nbsp;&nbsp;├─ ', '&nbsp;&nbsp;&nbsp;└─ ');
        $tree->nbsp = '&nbsp;&nbsp;&nbsp;';
        foreach ($result as $r) {
            $r['str_manage'] = '<a href="' . U("Platform/shopnav_add", array("parentid" => $r['id'],"cid"=>$r['cid'])) . '"  class="btn btn-primary btn-xs">'.添加子菜单.'</a>  <a href="' . U("Platform/shopnav_edit", array("id" => $r['id'],"parentid"=>$r['parentid'],"cid"=>$r['cid'])) . '"  class="btn btn-info btn-xs">'.编辑.'</a>  <a class="js-ajax-delete btn btn-warning btn-xs" href="' . U("Platform/shopnav_del", array("id" => $r['id'])) . '">'.删除.'</a> ';
            $r['status'] = $r['status'] ?显示 : 隐藏;
            $path=explode('-',$r['path']);
            $num=count($path);
            if($num==3){
                if($r['istop']==1){
                 $r['istop'] ='<a href="'.U("Platform/shopnav_top",array('id' => $r['id'])).'" class="btn btn-danger btn-xs">是</a>';
                }else{
                 $r['istop'] ='<a href="'.U("Platform/shopnav_top",array('id' => $r['id'])).'" class="btn btn-primary btn-xs">否</a>';
                }
            }else{
                $r['istop'] ='<a href="javascript:;" class="btn btn-default btn-xs">禁止设置</a>';
            }
            
            if($num==4){
                if($r['islight']==1){
                     $r['islight'] ='<a href="'.U("Platform/shopnav_light",array('id' => $r['id'])).'"  class="btn btn-danger btn-xs">高亮</a>';
                }else{
                     $r['islight'] ='<a href="'.U("Platform/shopnav_light",array('id' => $r['id'])).'"  class="btn btn-primary btn-xs">正常</a>';
                }
            }else{
                $r['islight'] ='<a href="javascript:;" class="btn btn-default btn-xs">禁止设置</a>';
            }
            
            $array[] = $r;
        }
    
        $tree->init($array);
        $str = "<tr>
                <td><input name='listorders[\$id]' type='text' size='3' value='\$listorder' class='input input-order'></td>
                <td>\$id</td>
                <td >\$spacer\$label</td>
                <td>\$status</td>
                <td style='text-align: center;'>\$istop</td>
                <td style='text-align: center;'>\$islight</td>
                <td>\$str_manage</td>
            </tr>";
        $categorys = $tree->get_tree(0, $str);
        $this->assign("categorys", $categorys);
        
        $cats=$this->navcat_model->select();
        $this->assign("navcats",$cats);
        $this->assign("navcid",$cid);
       
        $this->assign('title','导航管理');
        $this->display();
    }
    
   function shopnav_light(){
        $id=$_GET['id'];
        $rs=$this->nav_model->find($id);
        if($rs['islight']==0){
            $data['islight']=1;
        }else{
            $data['islight']=0;
        }
        $data['id']=$id;
        $this->nav_model->save($data);
        $this->redirect('Platform/shopnav');

   }

   function shopnav_top(){
        $id=$_GET['id'];
        $rs= $this->nav_model->find($id);
        if($rs['istop']==0){
            $data['istop']=1;
        }else{
            $data['istop']=0;
        }
        $data['id']=$id;

        $this->nav_model->save($data);
        $this->redirect('Platform/shopnav');
        
   }
    function shopnav_add(){
        if($_SESSION['nav']['cid']!=null){
            $cid=$_SESSION['nav']['cid']; 
        }else{
            $cid=$_REQUEST['cid'];
        }
        
        $result = $this->nav_model->where("cid=$cid")->order(array("listorder" => "ASC"))->select();
        import("Think.Tree");
        $tree = new \Tree();
        $tree->icon = array('&nbsp;│ ', '&nbsp;├─ ', '&nbsp;└─ ');
        $tree->nbsp = '&nbsp;';
        $parentid=I("get.parentid");
        foreach ($result as $r) {
            $r['str_manage'] = '<a href="' . U("Menu/add", array("parentid" => $r['id'], "menuid" => I("get.menuid"))) . '">添加子菜单</a> | <a href="' . U("Menu/edit", array("id" => $r['id'], "menuid" => I("get.menuid"))) . '">修改</a> | <a class="js-ajax-delete" href="' . U("Menu/delete", array("id" => $r['id'], "menuid" => I("get.menuid"))) . '">删除</a> ';
            $r['status'] = $r['status'] ? "显示" : "隐藏";
            $r['selected'] = $r['id']==$parentid?"selected":"";
            $array[] = $r;
        }
            
        $tree->init($array);
        $str = "<tr>
                <td><input name='listorders[\$id]' type='text' size='3' value='\$listorder' class='input'></td>
                <td>\$id</td>
                <td >\$spacer\$label</td>
                <td>\$status</td>
                <td>\$str_manage</td>
            </tr>";
        $str="<option value='\$id' \$selected>\$spacer\$label</option>";
        $nav_trees = $tree->get_tree(0, $str);
        $this->assign("nav_trees", $nav_trees);
            
        $ticketType = D('TicketType');
        $ticketType_array = $ticketType->where('ishidden=0')->order('bids ASC')->select();
        $this->assign("ticketType_array",$ticketType_array);
        $article_terms = D('article_terms');
        $article_terms_array = $article_terms->where('parent=0')->select();
        foreach($article_terms_array as $key =>$value){
        	$child_list=$article_terms->where('parent='.$value['term_id'])->select();
	  		$article_terms_array[$key]['child']=$child_list;
        }
        $this->assign("article_terms_array",$article_terms_array);
        
        $article_posts = D('article_posts');
        $article_posts_array=$article_posts->where('post_type=1')->select();
        $this->assign("article_posts_array",$article_posts_array);
        
        $cats=$this->navcat_model->select();
        $this->assign("navcats",$cats);
        $this->assign('title','添加导航');
        $this->assign("navcid",$cid);
        $this->display();

    }

   public function shopnav_add_post() {
        if (IS_POST) {
            $data=I("post.");
            $data['href']=htmlspecialchars_decode($data['href']);
            if($data['label']==null){
                 echo '0|||请输入导航名称|||';die();
            }
            if($data['href']=='http://'||$data['href']==null){
                 echo '0|||请输入导航链接|||';die();
            }
            if ($this->nav_model->create($data)) {
                $result=$this->nav_model->add();
                if ($result!==false) {
                    $parentid = $_POST['parentid']==0?"0":$_POST['parentid'];
                    if(empty($parentid)){
                        $data['path']="0-$result";
                    }else{
                        $parent=$this->nav_model->where("id=$parentid")->find();
                        $data['path']=$parent[path]."-$result";
                    }
                    $data['id']=$result;
                    $this->nav_model->save($data);
                    echo '1|||添加成功|||继续操作|||reload|||返回列表|||/Platform/shopnav.html';die();
                  
                } else {
                    echo '0|||添加失败！|||';die();
                  
                }
            } else {
                echo '0|||添加失败！|||';die();
            }
        }
    }
    function shopnav_edit(){
        $cid=intval($_REQUEST['cid']);
        $id=intval(I("get.id"));
        $result = $this->nav_model->where("cid=$cid and id!=$id")->order(array("listorder" => "ASC"))->select();
         import("Think.Tree");
        $tree = new \Tree();
        $tree->icon = array('&nbsp;│ ', '&nbsp;├─ ', '&nbsp;└─ ');
        $tree->nbsp = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
        $parentid= I("get.parentid");
        foreach ($result as $r) {
            $r['str_manage'] = '<a href="' . U("Menu/add", array("parentid" => $r['id'], "menuid" => I("get.menuid"))) . '">添加子菜单</a> | <a href="' . U("Menu/edit", array("id" => $r['id'], "menuid" => I("get.menuid"))) . '">修改</a> | <a class="js-ajax-delete" href="' . U("Menu/delete", array("id" => $r['id'], "menuid" => I("get.menuid"))) . '">删除</a> ';
            $r['status'] = $r['status'] ? "显示" : "隐藏";
            $r['selected'] = $r['id']==$parentid?"selected":"";
            $array[] = $r;
        }
        
        $tree->init($array);
        $str = "<tr>
                <td><input name='listorders[\$id]' type='text' size='3' value='\$listorder' class='input'></td>
                <td>\$id</td>
                <td >\$spacer\$label</td>
                <td>\$status</td>
                <td>\$str_manage</td>
            </tr>";
        $str="<option value='\$id' \$selected>\$spacer\$label</option>";
        $nav_trees = $tree->get_tree(0, $str);
        $this->assign("nav_trees", $nav_trees);
        
         $ticketType = D('TicketType');
        $ticketType_array = $ticketType->where('ishidden=0')->order('bids ASC')->select();
        $this->assign("ticketType_array",$ticketType_array);
        
        $article_terms = D('article_terms');
        $article_terms_array = $article_terms->where('parent=0')->select();
        foreach($article_terms_array as $key =>$value){
        	$child_list=$article_terms->where('parent='.$value['term_id'])->select();
	  		$article_terms_array[$key]['child']=$child_list;
        }
        $this->assign("article_terms_array",$article_terms_array);
        
        $article_posts = D('article_posts');
        $article_posts_array=$article_posts->where('post_type=1')->select();
        $this->assign("article_posts_array",$article_posts_array);
        
        $cats=$this->navcat_model->select();
        $this->assign("navcats",$cats);
            
        $nav=$this->nav_model->where("id=$id")->find();
        $nav['hrefold'] = stripslashes($nav['href']);
        $href = unserialize($nav['hrefold']);
        if(empty($href)){
            if($nav['hrefold'] == "home"){
                $href = __ROOT__."/";
            }else{
                $href = $nav['hrefold'];
            }
        }else{
            $default_app = strtolower(C("DEFAULT_GROUP"));
            $href = U($href['action'],$href['param']);
            $g = C("VAR_GROUP");
            $href = preg_replace("/\/$default_app\//", "/",$href);
            $href = preg_replace("/$g=$default_app&/", "",$href);
        }
            
        $nav['href'] = $href;
        $this->assign($nav);
       $this->assign('title','编辑导航');
        $this->assign("navcid",$cid);
        $this->display();
    }

    function shopnav_edit_post(){
           if (IS_POST) {
            $parentid=empty($_POST['parentid'])?"0":$_POST['parentid'];
            if(empty($parentid)){
                $_POST['path']="0-".$_POST['id'];
            }else{
                $parent=$this->nav_model->where("id=$parentid")->find();
                    
                $_POST['path']=$parent['path']."-".$_POST['id'];
            }
            
            $data=I("post.");
            if($data['label']==null){
                 echo '0|||请输入导航名称|||';die();
            }
            if($data['href']=='http://'||$data['href']==null){
                 echo '0|||请输入导航链接|||';die();
            }
            $data['href']=htmlspecialchars_decode($data['href']);
            if ($this->nav_model->create($data)) {
                if ($this->nav_model->save() !== false) {
                    echo '1|||保存成功！|||继续操作|||reload|||返回列表|||/Platform/shopnav.html';die();
                   
                } else {
                    echo '0|||保存失败！|||';die();
                
                }
            } else {
                echo '0|||保存失败！|||';die();
            }
        }     
    }

    /**
     * 排序
     */
    public function shopnav_listorders() {
        $status = $this->_listorders($this->nav_model);
        if ($status) {
            $this->success("排序更新成功！");
        } else {
            $this->error("排序更新失败！");
        }
    }
    function shopnav_del(){
        $id = intval(I("get.id"));;
        $count = $this->nav_model->where(array("parentid" => $id))->count();
        if ($count > 0) {
            $this->error("该菜单下还有子菜单，无法删除！");
        }
        if ($this->nav_model->delete($id)!==false) {
            $this->success("删除菜单成功！");
        } else {
            $this->error("删除失败！");
        }
    }

      /**
     *  排序 排序字段为listorders数组 POST 排序字段为：listorder
     */
    protected function _listorders($model) {
        if (!is_object($model)) {
            return false;
        }
        $pk = $model->getPk(); //获取主键名称
        $ids = $_POST['listorders'];
        foreach ($ids as $key => $r) {
            $data['listorder'] = $r;
            $model->where(array($pk => $key))->save($data);
        }
        return true;
    }

    // 清除缓存
    public function clearcache(){
        $dir=RUNTIME_PATH;
        $this->deldir($dir);
        $this->success("清除缓存成功！");
        
    }

  
    function deldir($dir) {
        
        $dh = opendir($dir);
        while ($file = readdir($dh)) {
            if ($file != "." && $file != "..") {
                $fullpath = $dir . "/" . $file;
                if (!is_dir($fullpath)) {
                    unlink($fullpath);
                } else {
                   $this->deldir($fullpath);
                }
            }
        }
    }
    
    
    function ad(){
    	$ad=M('ad');
    	$rs=$ad->select();
    	$this->assign('rs',$rs);
    	$this->assign('title','网站碎片');
		$this->display();
    }
     function ad_add(){
    	if(IS_POST){
    		$ad=M('ad');
			$data=I('post.');
			if($data['ad_id']){
				$rs=$ad->save($data);
			}else{
				$rs=$ad->add($data);
			}
			
			if($rs){
				 echo '1|||添加成功|||继续操作|||reload|||返回列表|||/Platform/ad.html';die();
			}else{
				 echo '0|||添加失败！|||';die();
			}
		}else{
			$id=$_GET['id'];
			if($id){
				$ad=M('ad');
				$rs=$ad->find($id);
			}
			$this->assign('ad',$rs);
			$this->assign('title','添加网站碎片');
			$this->display();
		}
    }
    function ad_del(){
    	$ad=M('ad');
    	$id=$_GET['id'];
		$rs=$ad->delete($id);
		$this->redirect('Platform/ad');
    }

 
    
}